﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace SeparateProjects
{
    public partial class Pro_User_Login : Form
    {
        //申明一个bool的值
        public bool IsStudent = true;
        public Pro_User_Login()
        {
            InitializeComponent();
        }
        #region 登陆界面面跳转注册界面
        /// <summary>
        /// 登陆页面跳转注册页面，登陆页面隐藏
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lbl_ZhuCe_Click(object sender, EventArgs e)
        {
            Pro_User_Registration obj = new Pro_User_Registration();
            obj.Show();
            this.Hide();
        }
        #endregion
        #region  登陆验证
        /// <summary>
        /// 登陆验证
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Login_Click(object sender, EventArgs e)
        {
            var UserName = txt_UserName.Text.Trim();
            var Password = txt_Password.Text.Trim();
            if (string.IsNullOrWhiteSpace(UserName))
            {
                MessageBox.Show("用户名不能为空", "提示信息",
                                 MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                this.txt_UserName.Focus();
            }
            if (string.IsNullOrWhiteSpace(Password))
            {
                MessageBox.Show("密码不能为空", "提示信息",
                                 MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                this.txt_Password.Focus();
            }
            if (IsStudent.Equals(false))
            {
                //MessageBox.Show("管理员");
                string str = @"Data Source = DESKTOP-28VSNS5;
                                Initial Catalog = Training_Project;
                                Uid = Lv;Pwd = 123456;";
                var coon = new SqlConnection(str);
                try
                {
                    coon.Open();
                    var sql = string.Format(@"select count(0) from Manager_Login
                                            where UserName='{0}' and Password = '{1}'",UserName,Password);
                    var comm = new SqlCommand(sql, coon);
                    object obj = comm.ExecuteScalar();
                    int number = Convert.ToInt32(obj);
                    if (number > 0)
                    {
                        Pro_Admin_Index page = new Pro_Admin_Index(txt_UserName.Text);
                        page.Show();
                        this.Hide();

                    }
                    else
                    {
                        MessageBox.Show("输入密码与用户不匹配，请重新输入","提示信息",
                                            MessageBoxButtons.OKCancel,
                                            MessageBoxIcon.Information);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("服务器异常，请联系管理员"+ex.Message);
                }
                finally
                {
                    coon.Close();
                }
            }
            else 
            {
                //MessageBox.Show("学员");
                string str = @"Data Source = DESKTOP-28VSNS5;
                                Initial Catalog = Training_Project;
                                Uid = Lv;Pwd = 123456;";
                var coon = new SqlConnection(str);
                try
                {
                    coon.Open();
                    var sql = string.Format(@"select count(0) from Stu_Login
                                            where UserName='{0}' and Password = '{1}'", UserName, Password);
                    var comm = new SqlCommand(sql, coon);
                    object obj = comm.ExecuteScalar();
                    int number = Convert.ToInt32(obj);
                    if (number > 0)
                    {
                        Pro_User_Index page = new Pro_User_Index(txt_UserName.Text);
                        page.Show();
                        this.Hide();
                    }
                    else
                    {
                        MessageBox.Show("输入密码与用户不匹配，请重新输入", "提示信息",
                                            MessageBoxButtons.OKCancel,
                                            MessageBoxIcon.Information);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("服务器异常，请联系管理员" + ex.Message);
                }
                finally
                {
                    coon.Close();
                }
            }
        }
        #endregion
        #region  点击按钮切换颜色
        /// <summary>
        /// 设置切换点击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_User_Click(object sender, EventArgs e)
        {
            if ((btn_User.Focused) == IsStudent)
            {
                //点击切换后的颜色
                this.btn_User.BackColor = Color.FromArgb(57, 55, 51);
                //原来颜色
                this.btn_Admin.BackColor = Color.FromArgb(122, 125, 126);
            }
            IsStudent = true;
            txt_UserName.Clear();
            txt_Password.Clear();

        }

        private void btn_Admin_Click(object sender, EventArgs e)
        {
            if ((btn_Admin.Focused) == IsStudent)
            {
                //点击切换后的颜色
                btn_Admin.BackColor = Color.FromArgb(57, 55, 51);
                //原来颜色
                this.btn_User.BackColor = Color.FromArgb(122, 125, 126);
            }
            IsStudent = false;
            txt_UserName.Clear();
            txt_Password.Clear();
        }
        #endregion
        #region  键盘按下点击事件
        /// <summary>
        /// 键盘按下点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txt_UserName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue.Equals(13))
            {
                this.btn_Login_Click(null,null);
            }
        }
        #endregion
    }
}
